/**
ProductZtreeFunction.java
business
2012-8-10上午8:56:14
@author 屈甲康
@QQ285799123
mailto:qujiakang@126.com
**/
package org.jackysoft.report.google.function;

import java.util.Collection;
import javax.annotation.Nullable;

import org.jackysoft.entity.Department;
import org.jackysoft.entity.Product;
import org.jackysoft.google.collection.AbstractZTreeFunction;
import org.jackysoft.google.collection.SimplifyDepartmentFunction;
import org.jackysoft.google.collection.ZTreeNode;
import org.jackysoft.service.ProductService;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;

public class ProductZtreeFunction extends AbstractZTreeFunction<Product, ZTreeNode> {
	
	private ProductService productService;
	public ProductZtreeFunction(ProductService pservice){
		this.productService = pservice;
	}
	

	@Override
	public ZTreeNode apply(@Nullable Product from) {
	
		Collection<Department> depts = from.getProviders();
		Collection<Department> jdepts = Lists.newArrayList();
		int size = productService.getQuotaCount(from.getCode());
		ZTreeNode zt = new ZTreeNode();
		zt.setId(from.getCode());
		zt.setKey(from.getCode());
		zt.setName(from.getCode()+"["+from.getName()+"]-["+size+"]");
		zt.setLeaf(false);
		zt.setIsParent("true");
		
	    zt.attr("code", from.getCode())
	      .attr("ptype", from.getItemType().name())
	      .attr("onquota", false)
	      .attr("csize", size);
	    if(depts!=null && !CollectionUtils.isEmpty(depts)){
	    	jdepts = Collections2.transform(depts, new SimplifyDepartmentFunction());
	    }
	    zt.attr("providers", jdepts);
	    return zt;
	}

}
